﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sqlStu = "select * from Articles";

            var dt = DbHelper.GetDataTable(sqlStu);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;  //使表只读不可修改 此时单击将只选择一个单元格
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  //通过单击某个单元格将选择整行
            dataGridView1.AllowUserToAddRows = false;   //去除表格中底部一行
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var Author = textBox1.Text;
            var sql = string.Format("select * from Articles where Author like '%{0}%'", Author);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editform = new EditForm();
            var res = editform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                //更新当前数据
                var Title = textBox1.Text;
                var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", Title);
                var dt = DbHelper.GetDataTable(sqlStu);

                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("这个是No");
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                //var class_id = (int)dataGridView1.SelectedRows[0].Cells["class_id"].Value;

                EditForm editform = new EditForm(Id, Title, Author, CreatedTime);
                var res = editform.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    //更新当前数据
                    var TitleTemp = textBox1.Text;
                    var sqlStu = string.Format("select * from Articles where  like '%{0}%'", TitleTemp);
                    var dt = DbHelper.AddOrEditOrDeleteSave(sqlStu);

                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("这个是No");
                }

            }
            else
            {
                MessageBox.Show("您未选择任何数据行", "提示");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var row = dataGridView1.SelectedRows[0];

            var Id = (int)row.Cells["Id"].Value;

            if (Id > 0)
            {
                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", Id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        //更新当前数据
                        var Title = textBox1.Text;
                        var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", Title);
                        var dt = DbHelper.GetDataTable(sqlStu);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功", "提示");
                    }
                }

                else
                {
                    MessageBox.Show("没有选择任何行", "提示");
                }
            }
        }
    }
}
